package com.base.cn.platform.os.controller.statistics;

import com.base.cn.platform.os.common.AdminBaseController;
import com.base.cn.platform.os.common.constants.ConfigName;
import com.base.cn.platform.os.common.utils.result.ResultUtil;
import com.base.cn.platform.os.service.manage.config.SysConfigService;
import com.base.cn.platform.os.service.manage.statistics.flow.FlowStatisticsService;
import com.base.cn.platform.os.service.manage.statistics.flow.ZStatisticsTerminalUVCountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;


/**
 * 流量统计Controller
 *
 * @author hackeridaer
 */
@Controller
@RequestMapping("/admin/stat")
public class FlowStatController extends AdminBaseController {

    @Autowired
    private FlowStatisticsService flowStatisticsService;
    @Autowired
    private ZStatisticsTerminalUVCountService zStatisticsTerminalUVCountService;
    @Autowired
    private SysConfigService sysConfigService;

    @RequestMapping("/flow")
    public String flow(Model model){
        TreeMap<String,String> configMap = sysConfigService.getSysConfigContext(ConfigName.VLASPROVIDER);
        model.addAttribute("configMap",configMap);
        return resultPage("/statistics/flow/flow-statistics");
    }

    /**
     * 流量看板
     * @param type
     * @param startTime
     * @param endTime
     * @return Map<String,Object>
     */
    @RequestMapping("/flowBulletinBoard")
    @ResponseBody
    public Map<String,Object> flowBulletinBoard(@RequestParam(value = "type",required = false,defaultValue = "1") int type,
                                                @RequestParam(value = "startTime",required = false) Date startTime,
                                                @RequestParam(value = "endTime",required = false) Date endTime){
        Map<String,Object> params =new HashMap<>();
        params.put("type",type);
        params.put("startTime",startTime);
        params.put("endTime",endTime);
        return flowStatisticsService.flowBulletinBoard(params);
    }

    /**
     * 录播流量统计（96K）
     * @param type
     * @param startTime
     * @param endTime
     * @return
     */
    @RequestMapping("/queryVideoFlowCountData")
    @ResponseBody
    public Map<String,Object> queryVideoFlowCountData(@RequestParam(value = "type",required = false,defaultValue = "1") int type,
                                                      @RequestParam(value = "startTime",required = false) Date startTime,
                                                      @RequestParam(value = "endTime",required = false) Date endTime){
        Map<String,Object> params =new HashMap<>();
        params.put("type",type);
        params.put("startTime",startTime);
        params.put("endTime",endTime);
        return flowStatisticsService.queryVideoFlowCountData(params);
    }

    /**
     * 终端系统分布
     * @param type
     * @param startTime
     * @param endTime
     * @return Map<String,Object>
     */
    @RequestMapping("/queryTerminalUVCountData")
    @ResponseBody
    public Map<String,Object> queryTerminalUVCountData(@RequestParam(value = "type",required = false,defaultValue = "1") int type,
                                                       @RequestParam(value = "startTime",required = false) Date startTime,
                                                       @RequestParam(value = "endTime",required = false) Date endTime){
        Map<String,Object> params =new HashMap<>();
        params.put("type",type);
        params.put("startTime",startTime);
        params.put("endTime",endTime);
        Map<String,Object> data = zStatisticsTerminalUVCountService.queryTerminalUVCountData(params);
        return ResultUtil.SUCCESS(data);
    }

    /**
     * 渠道分布(PV)
     * @param type
     * @param startTime
     * @param endTime
     * @return Map<String,Object>
     */
    @RequestMapping("/queryPageViewCountSpread")
    @ResponseBody
    public Map<String,Object> queryPageViewCountSpread(@RequestParam(value = "type",required = false,defaultValue = "1") int type,
                                                       @RequestParam(value = "startTime",required = false) Date startTime,
                                                       @RequestParam(value = "endTime",required = false) Date endTime){
        Map<String,Object> params =new HashMap<>();
        params.put("type",type);
        params.put("startTime",startTime);
        params.put("endTime",endTime);
        Map<String,Object> data = flowStatisticsService.queryPageViewCountSpread(params);
        return ResultUtil.SUCCESS(data);
    }
}
